package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.CartMapper;
import com.aaa.mrcai.pojo.entity.UserInfo;
import com.aaa.mrcai.pojo.vo.CartVO;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;

/**
 * @ Author: Lining
 * @ Date: 2021/12/17 19:14
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ServletContext servletContext;

    @Override
    public AjaxResult addCart(CartVO cartVO) {
        Long cartExist = cartMapper.isCartExist(cartVO);
        if(cartExist>0){
            return AjaxResult.defeated("对不起，您已将此商品加入购物车");
        }
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        cartVO.setUid(user.getUserLogin().getUid());
        int i = cartMapper.addcart(cartVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
}
